package dome2;

import java.beans.FeatureDescriptor;
import java.lang.reflect.Field;

public class MyReflect {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
        //1.获取字节码文件
        Class clazz = Class.forName("dome2.Student");

        //2.获取公共成员变量
        Field[] fields = clazz.getFields();
        for (Field x : fields) {
            System.out.println(x);
        }

        //获取单个成员变量
        Field name = clazz.getDeclaredField("name");
        System.out.println(name);

        //获取成员变量名字
        String n = name.getName();//获取成员变量名
        System.out.println(n);

        //获取成员变量的数据类型
        Class<?> type = name.getType();
        System.out.println(type);

        //获取成员变量记录的值 和对象有关，所以先初始化一个
        Student s = new Student("张三",23,"男");
        name.setAccessible(true);//临时取消访问权限
        String value = (String) name.get(s);//获取该对象name记录的值
        System.out.println(value);

        //修改对象里面记录的值
        name.set(s, "lisi");
        System.out.println(s);
    }
}
